package com.sskj.givecoin.p.activity;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.sskj.givecoin.MyAppLication;
import com.sskj.givecoin.common.Constants;
import com.sskj.givecoin.m.HttpConfig;
import com.sskj.givecoin.m.bean.CoinFeeBean;
import com.sskj.givecoin.m.bean.HttpData;
import com.sskj.givecoin.m.http.JsonCallBack;
import com.sskj.givecoin.p.base.BasePresenter;
import com.sskj.givecoin.utils.MD5Util;
import com.sskj.givecoin.utils.SPUtils;
import com.sskj.givecoin.utils.ToastUtil;
import com.sskj.givecoin.v.activity.WithdrawActivity;

/**
 * <pre>
 *     author : wk_math
 *     e-mail : wk_math@163.com
 *     time   : 2018/03/30
 *     desc   :
 *     version: 1.0
 * </pre>
 */

public class WithdrawPresenter extends BasePresenter<WithdrawActivity> {

    public void sendCode() {
        String phone = (String) SPUtils.get(MyAppLication.getApplication(), Constants.SP_MOBILE, "");
        OkGo.<HttpData>post(HttpConfig.BASE_URL + HttpConfig.SMS_SEND)
                .params("mobile", phone)
                .params("type", 5)
                .execute(new JsonCallBack<HttpData>(this) {
                    @Override
                    public void _onSuccess(Response<HttpData> response) {
                        HttpData httpListData = response.body();
                        if (httpListData.getStatus() == 200) {
                            mView.startCount();
                        } else {
                            ToastUtil.showShort(httpListData.getMsg());
                        }
                    }
                });
    }

    public void sendEmail() {
        OkGo.<HttpData>post(HttpConfig.BASE_URL + HttpConfig.SEND_EMAIL)
                .params("email", MyAppLication.getMobile())
                .execute(new JsonCallBack<HttpData>(this) {
                    @Override
                    public void _onSuccess(Response<HttpData> response) {
                        HttpData httpListData = response.body();
                        if (httpListData.getStatus() == 200) {
                            mView.startCount();
                        } else {
                            ToastUtil.showShort(httpListData.getMsg());
                        }
                    }
                });
    }

    public void checkCode(String code) {
        OkGo.<HttpData>post(HttpConfig.BASE_URL + HttpConfig.CHECK_CODE)
                .params("mobile", MyAppLication.getMobile())
                .params("code", code)
                .execute(new JsonCallBack<HttpData>() {
                    @Override
                    public void _onSuccess(Response<HttpData> response) {
                        HttpData httpListData = response.body();
                        if (httpListData.getStatus() == 200) {
                            mView.setCodeOk();
                        } else {
                            ToastUtil.showShort(httpListData.getMsg());
                        }
                    }
                });
    }

    public void withdraw(String num, String tpwd, String code, String qianbao_url, int assetId) {
        OkGo.<HttpData>post(HttpConfig.BASE_URL + HttpConfig.WITHDRAW)
                .params("mobile", MyAppLication.getMobile())
                .params("num", num)
                .params("tpwd", MD5Util.md5FiveTime(tpwd))
                .params("code", code)
                .params("qianbao_url", qianbao_url)
                .params("pid", assetId)
                .execute(new JsonCallBack<HttpData>(this) {
                    @Override
                    public void _onSuccess(Response<HttpData> response) {
                        HttpData httpListData = response.body();
                        ToastUtil.showShort(httpListData.getMsg());
                        if (httpListData.getStatus() == 200) {
                            mView.finish();
                        }
                    }
                });
    }

    public void getCoinFee(int pid) {
        OkGo.<HttpData<CoinFeeBean>>get(HttpConfig.BASE_URL + HttpConfig.GET_COIN_FEE)
                .params("pid", pid)
                .execute(new JsonCallBack<HttpData<CoinFeeBean>>(this) {
                    @Override
                    public void _onSuccess(Response<HttpData<CoinFeeBean>> response) {
                        HttpData<CoinFeeBean> httpData = response.body();
                        if (httpData.getStatus() == 200) {
                            mView.updateCoin(httpData.getData());
                        }
                    }
                });
    }
}
